﻿#!/usr/bin/env python3

from  difflib import Differ, unified_diff, ndiff, context_diff
import sys

def main():
    '''
difflib -
比对找出两个序列的差异, 提供了4种比对,
1.unified_diff,  结果使用unified_diff格式输出. 整个比对输出
2.ndiff,   ndiff格式, 一行行比对
3.HtmlDiff
4.context_diff,     context diff 格式. 行不同

    '''
    print(main.__doc__)

    text1 = '''  1. Beautiful is better than ugly.
    2. Explicit is better than implicit.
    3. Simple is better than complex.
    4. Complex is better than complicated.
    '''.splitlines(keepends=True)
    
    text2 = '''  1. Beautiful is better than ugly.
    3.   Simple is better than complex.
    4. Complicated is better than complex.
    5. Flat is better than nested.
    '''.splitlines(keepends=True)

    print("="*20 + "Differ")
    d = Differ()
    res = list(d.compare(text1, text2))
    sys.stdout.writelines(res)

    print("="*20 + "unified_diff")
    sys.stdout.writelines(unified_diff(text1, text2, fromfile="a", tofile="b"))
    
    print("="*20 + "ndiff")
    sys.stdout.writelines(ndiff(text1, text2))

    print("="*20 + "context_diff")
    sys.stdout.writelines(context_diff(text1, text2, fromfile="a", tofile="b"))





if __name__=='__main__':
    main()


